import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Router from 'vue-router';

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: () => import('../views/Home.vue'),
        meta: { title: '首页'}
    },
    {
        path: '/category/:cate',
        name: 'category',
        component: () => import('../views/Home.vue'),
        meta: { title: '分类', params: 'cate'}
    },
    {
        path: '/search/:words',
        name: 'search',
        component: () => import('../views/article/searchArticle.vue'),
        meta: { title: '搜索', params: 'words'}
    },
    {
        path: '/about',
        name: 'about',
        component: () => import('../views/About.vue'),
        meta: { title: '关于'}
    },
    {
        path: '/friend',
        name: 'friend',
        component: () => import('../views/Friend.vue'),
        meta: { title: '友链'}
    },
    {
        path: '/article/:id',
        name: 'article',
        component: () => import('../views/Articles.vue'),
        meta: { title: '文章'}
    },
	{
		path: '/login',
		name: 'login',
		component: () => import('../views/login/Login.vue'),
		meta: {title: '登录'}
	},
	{
		path: '/phoneLogin',
		name: 'login',
		component: () => import('../views/login/phoneLogin.vue'),
		meta: {title: '登录'}
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('../views/login/register.vue'),
		meta: {title: '注册'}
	},
	{
		path: '/articleType/:id',
		name: 'articleType',
		component: () => import('../views/article/articleType.vue'),
		meta: {title: '文章分类列表'}
	},
	{
		path: '/my',
		name: 'my',
		component: () => import('../views/my/my.vue'),
		meta: {title: '我的'}
	},
	{
		path: '/myInfo',
		name: 'myInfo',
		component: () => import('../views/my/myInfo.vue'),
		meta: {title: '我的信息'}
	},
	{
		path: '/myClub',
		name: 'myClub',
		component: () => import('../views/my/myClub.vue'),
		meta: {title: '我的社团'}
	},
	{
		path: '/manager',
		name: 'manager',
		component: () => import('../views/manager/manager.vue'),
		meta: {title: '我的管理'}
	},
	{
		path: '/myArticle',
		name: 'myArticle',
		component: () => import('../views/my/myArticle.vue'),
		meta: {title: '我的文章'}
	},
	{
		path: '/race',
		name: 'race',
		component: () => import('../views/race/race.vue'),
		meta: {title: '比赛'}
	},
	{
		path: '/myRace',
		name: 'myRace',
		component: () => import('../views/my/myRace.vue'),
		meta: {title: '我的比赛'}
	},
	{
		path: '/department',
		name: 'department',
		component: () => import('../views/manager/department.vue'),
		meta: {title: '部门管理'}
	},
	{
		path: '/posts',
		name: 'posts',
		component: () => import('../views/manager/posts.vue'),
		meta: {title: '职位管理'}
	},
	{
		path: '/raceManager',
		name: 'raceManager',
		component: () => import('../views/manager/race.vue'),
		meta: {title: '比赛管理'}
	},
	{
		path: '/club',
		name: 'club',
		component: () => import('../views/club/club.vue'),
		meta: {title: '社团'}
	},
	{
		path: '/insertArticle',
		name: 'insertArticle',
		component: () => import('../views/article/insertArticle.vue'),
		meta: {title: '添加文章'}
	},
	{
		path: '/articleManager',
		name: 'articleManager',
		component: () => import('../views/manager/articleManager.vue'),
		meta: {title: '文章管理'}
	},
	{
		path: '/userManager',
		name: 'userManager',
		component: () => import('../views/manager/user.vue'),
		meta: {title: '人员申请管理'}
	},
	{
		path: '/managerLogin',
		name: 'managerLogin',
		component: () => import('../views/login/managerLogin.vue'),
		meta: {title: '管理员登录'}
	},
	{
		path: '/managerClub',
		name: 'managerClub',
		component: () => import('../views/manager/managerClub.vue'),
		meta: {title: '社团申请管理'}
	},
	{
		path: '/myRecord',
		name: 'myRecord',
		component: () => import('../views/my/myRecord.vue'),
		meta: {title: '我的历史文章'}
	},
	{
		path: '/myCart',
		name: 'myCart',
		component: () => import('../views/my/myCart.vue'),
		meta: {title: '我的收藏文章'}
	},
	{
		path: '/myLike',
		name: 'myLike',
		component: () => import('../views/my/myLike.vue'),
		meta: {title: '我的点赞文章'}
	},
	{
		path: '/articleTypeManager',
		name: 'articleTypeManager',
		component: () => import('../views/manager/articleTypeManager.vue'),
		meta: {title: '文章类型管理'}
	}
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    let title = 'Gblog'
    if (to.meta.params){
        title = `${to.meta.title}:${to.params[to.meta.params] || ''} - ${title}`
    }else {
        title = `${to.meta.title} - ${title}`
    }
    document.title = title
    if (to.path !== from.path) {
        store.dispatch('setLoading', true);
    }
    next();
})
router.afterEach((to, from) => {
    // 最多延迟 关闭 loading
    setTimeout(() => {
        store.dispatch('setLoading', false);
    }, 1500)
})



export default router
